Skip to content

[2.x] Compare uniqueItems items without copying them - #1306

Merged
stevehu merged 3 commits into
networknt:2.xfrom
ItsDeadlyProgrammer:perf/1302-unique-items-2.x
Oct 13, 2026
Merged

stevehu merged 3 commits into
networknt:2.xfrom
ItsDeadlyProgrammer:perf/1302-unique-items-2.x

Conversation

@ItsDeadlyProgrammer

@ItsDeadlyProgrammer ItsDeadlyProgrammer commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1302 on 2.x. Same change as #1305.

UniqueItemsValidator now compares items through a small key that walks each node in place, instead of building a comparisonKey copy. Numbers are still compared by value, so [1, 1.0] is still a duplicate, but arrays without mixed number types no longer pay for it.

How numbers are compared:

  • A number's value is its decimalValue(), as before. For a double that is its shortest decimal form, so 1e30 equals 10^30 and a double read from 0.1 equals a BigDecimal 0.1.
  • Numbers whose value is whole and fits in a long (ints, longs, and whole doubles, BigIntegers and BigDecimals) are compared as longs.
  • Other doubles are compared as doubles.
  • Only BigIntegers and BigDecimals outside that range are compared as BigDecimals.
  • NaN, Infinity and -Infinity still compare as nodes.
  • A double hashes as Double.hashCode of its value. A BigInteger or BigDecimal outside the long range takes that same hash only when it is exactly a double's value; otherwise it hashes its own exact value. So equal numbers of different types land in the same bucket, while integers that only round to the same double, such as 2^70 and 2^70 + 1, don't. The hash never calls doubleValue() on a big node, which Jackson 3 would throw from for values like 10^400.
  • Objects hash the same regardless of property order, and numbers still never equal booleans or strings.

There is no behaviour change from the current code. Fuzzing 400,000 number pairs (large whole doubles, powers of ten, big integers near them, trailing-zero decimals, and each value in another type) against the current comparisonKey gives the same verdict for every pair, both at the top level and nested.

Items deferred from the #1301 review:

  1. decimalsWithDifferentScaleAreDuplicates now reads the instance with USE_BIG_DECIMAL_FOR_FLOATS, so 1.0 and 1.00 stay BigDecimals with different scales. On 2.x it passes even without numeric folding, because Jackson 2 strips trailing zeros when it reads with USE_BIG_DECIMAL_FOR_FLOATS, and DecimalNode equality ignores scale. It is kept so both branches have the same test.
  2. The comparisonKey Javadoc is rewritten for the new key, with the indentation fixed.

New tests in UniqueItemsNumericEqualityTest: double vs BigDecimal, 0 vs -0.0, numbers outside the long range, large whole doubles ([1e30, 10^30] is a duplicate, the long 2^60 and the double 2^60 are not), 1,000 consecutive integers from 2^70 (all unique, then one repeated), [10^400] nested and at the top level, and [1e400, 1e401] / [1e400, 1e400] with USE_BIG_DECIMAL_FOR_FLOATS, and object property order. On the first version of this PR, the large-double test failed on both branches and the 10^400 test threw on master. The 2^70 test checks correctness only, so it doesn't depend on timing.

Timing with schema {"uniqueItems":true}: 50 warm-up runs, then the best of 5 rounds of 20 runs, and the median of 3 JVM runs. Times in ms.

master, JDK 25:

Instance Before #1273 (node equality) Current (comparisonKey) This PR
200k ints 6.8 15.6 9.2
20k objects, 10 string fields 7.6 12.1 8.2
200k decimals (i.5) 11.1 62.0 21.0
20k nested arrays ([i, i+1.25, {"a":i}]) 1.4 4.7 1.8

2.x, JDK 17 (I don't have a Java 8 JDK here):

Instance Before #1301 (node equality) Current (comparisonKey) This PR
200k ints 7.2 19.4 8.4
20k objects, 10 string fields 4.0 11.2 7.9
200k decimals (i.5) 9.2 72.5 17.3
20k nested arrays 1.7 5.1 1.8

Against the current code it is 1.5–3x faster on master and 1.4–4.2x on 2.x. It's still a bit slower than plain node equality, mostly from one key object per item and classifying each number. The objects numbers were the noisiest between runs. None of these instances goes through the big-number hash.

Consecutive integers from 2^70, which share a double in runs of 2^18. The first two columns are from your review; the last is one run with no warm-up:

Items Current First version of this PR This PR
20,000 15 ms 3.7 s (master) / 4.6 s (2.x) 4 ms (master) / 19 ms (2.x)
100,000 26 ms (master) / 108 ms (2.x)

mvn verify passes, including the JSON Schema Test Suite: 8625 tests on JDK 17.

comparisonKey built a BigDecimal for every number and copied every
nested array and object, so all arrays paid for the numeric-equality
fix, not only arrays that mix integers and decimals. Wrap each item in
a key that walks the node and compares numbers by value instead: whole
numbers that fit in a long as longs, other doubles as doubles, and only
other BigIntegers and BigDecimals as BigDecimals.

Whole doubles at or above 2^63 are compared by their exact value, the
same as smaller whole doubles, rather than by their shortest decimal
form.

Make decimalsWithDifferentScaleAreDuplicates read BigDecimals so it no
longer passes without numeric folding, fix the comparisonKey Javadoc
indentation by rewriting it, and add tests for the new comparison paths.

Fixes networknt#1302
@stevehu

stevehu commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Same review as #1305, see #1305 (posted just now). The code here is the same apart from the Jackson imports.

On 2.x with Java 8:

  • Item 1 (the crash) doesn't happen, because Jackson 2 returns Infinity from doubleValue(). The hash change still applies, so the two branches stay identical.
  • Item 2 (quadratic time) does happen: 20,000 consecutive integers from 2^70 take 4.6 s, against 15 ms on base.
  • Item 3 (large-double verdicts) applies here too.
  • Item 5 is specific to this branch: decimalsWithDifferentScaleAreDuplicates passes here without numeric folding, so please adjust the description.

… doubles

A BIG number that isn't exactly a double's value now hashes its own exact
value, so integers that round to the same double no longer share a bucket.
Hashing from the BigDecimal also keeps the branch identical to master, where
Jackson 3 throws from doubleValue() outside the double range.

Whole doubles of 2^53 and above go back to comparing by decimalValue(), their
shortest decimal form, as before. Changing that belongs with const and enum
in networknt#1293.
@ItsDeadlyProgrammer

Copy link
Copy Markdown
Contributor Author

Same fix as #1305, see my reply there: c2cd3f9. Only the Jackson imports differ.

On 2.x, 20,000 consecutive integers from 2^70 now take 19 ms on JDK 17, and the fuzz against the old comparisonKey found no different verdicts. For item 5, I corrected the description: decimalsWithDifferentScaleAreDuplicates passes here without numeric folding, and it stays only so both branches have the same tests. mvn verify: 8625 tests.

@stevehu

stevehu commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Same review as #1305, see #1305 (posted just now). c2cd3f9 fixes items 1–3 here too. On Java 8, the suites pass and 20,000 consecutive integers from 2^70 take 33 ms.

The -2^63 problem described there also happens here on Java 8, and the same value > -0x1p63 change fixes it: the fuzz then has 0 differences. Once that's in on both PRs, I'm happy to merge them together.

@stevehu

stevehu commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Follow-up for items 4 and 6 is now #1307.

@ItsDeadlyProgrammer

Copy link
Copy Markdown
Contributor Author

Same fix as #1305, see my reply there: bc140e7. Only the Jackson imports differ. mvn verify on JDK 17: 8642 tests pass.

@stevehu stevehu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick turnaround. I re-reviewed ba114aa (#1305) and bc140e7 (#1306); the two are identical apart from the Jackson imports.

Verification

  • The -2^63 fix works. [-2^63, its shortest form] is a duplicate and [-2^63, 9223372036854775616] is unique, matching base and const. All 15 -2^63 edge cases match base on Java 8, 17, 21 and 25.
  • Revert check: with the previous commit's validator, minusTwoToSixtyThreeAsADoubleIsComparedByItsShortestDecimalForm fails on JDK 25 and Java 8.
  • On master (JDK 17, 25) and 2.x (Java 8, 21):
    • The JSON Schema Test Suite (7261), the UniqueItems tests (50 + 17), and the const/enum tests pass.
    • The 253k-input and 2.39M-input fuzzes show 0 disagreements.
    • The DoS probe stays at 4–20 ms for 20k items.

Optional, not blocking

  • The test Javadoc says the shortest form is -9223372036854776000 "on JDK 19+". Corretto 8 and 17 give the same form, so the JDK qualifier can go.
  • One more assertion would pin the pair on the boundary that moved: assertTrue(valid(array().add(Long.MIN_VALUE).add(-0x1p63))).
  • value > -0x1p63 relies on decimalValue() of -2^63 being below Long.MIN_VALUE. That holds on every JDK I tested. Deciding that single value from its decimal form would remove the assumption, if you ever touch this again.

Approving both; merging #1305 and #1306 together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants